package com.wsc.wisdomchina.activitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.activitity.introduction.fragment.LeaderListFragment;
import com.wsc.wisdomchina.activitity.introduction.fragment.OrganizationListFragment;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.javaBean.InfoDetailBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.StreetInfoListBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;

/**
 * 非列表详情
 * Created by ${wangtao} on 2017/3/2.
 */

public class PageInfoDetailActivity extends BaseActivity implements View.OnClickListener, ContributorView {

    private Context context;
    private TextView fragment_webView_title;
    private ImageView fragment_webView_back;
    private WebView fragment_webView_webView;
    private String id;
    private ContributorPresenter contributorPresenter;
    private final String PAGEDETAILFLAG = "PAGEDETAILFLAG";
    private String cidNumber = "";
    private String actualId = "";
    private ProgressBar pb_newsdetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pageinfodetail_layout);
        initView();
        context = this;
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        String name = intent.getStringExtra("name");
        String actualName = intent.getStringExtra("actualName");
        actualId = intent.getStringExtra("actualId");
        if (actualName != null) {
            fragment_webView_title.setText(actualName);
        } else {
            fragment_webView_title.setText(name);
        }

        contributorPresenter = new ContributorPresenter(this, context);
        String locationString = Tools.getSharedPreferencesStrings(Constant.SharePreferenceLocation, "", context);
        if (locationString.length() > 0) {
            StreetInfoListBean streetInfoListBean = JsonTools.jsonStringToObject(locationString, StreetInfoListBean.class);
            cidNumber = streetInfoListBean.getId();
        }
        if (id.equals("3704218965")) {
            //社区简介
            contributorPresenter.getStreetInfo(cidNumber, PAGEDETAILFLAG);
        } else if (id.equals("8316154972")) {
            //办公职能
            contributorPresenter.getOfficeDuty(cidNumber, PAGEDETAILFLAG);
        } else if (id.equals("8732441695")) {
            //网格管理
            contributorPresenter.getGridManagementList(cidNumber, PAGEDETAILFLAG);
        } else if (id.equals("5240197863")) {
            //社区特色
            contributorPresenter.getFeaturesDetail(actualId, PAGEDETAILFLAG);
        } else if (id.equals(LeaderListFragment.LEADER)) {
            //领导介绍
            contributorPresenter.getLeaderDetails(actualId, PAGEDETAILFLAG);
        } else if (id.equals(OrganizationListFragment.ORGANIZATION)) {
            //机构设置
            contributorPresenter.getOrganizationDetails(actualId, PAGEDETAILFLAG);
        }

    }

    private void initView() {
        fragment_webView_title = (TextView) findViewById(R.id.tv_headtitle);
        fragment_webView_back = (ImageView) findViewById(R.id.regist_back);
        fragment_webView_webView = (WebView) findViewById(R.id.fragment_webView_webView);
        fragment_webView_webView.getSettings().setDefaultTextEncodingName("UTF-8");
        fragment_webView_webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        fragment_webView_webView.getSettings().setLoadWithOverviewMode(false);
        fragment_webView_back.setOnClickListener(this);
        pb_newsdetail = (ProgressBar) findViewById(R.id.pb_newsdetail);
        pb_newsdetail.setVisibility(View.VISIBLE);
        fragment_webView_webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                pb_newsdetail.setProgress(newProgress);
                pb_newsdetail.postInvalidate();
                if (newProgress == 100) {
                    pb_newsdetail.setVisibility(View.GONE);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                // TODO Auto-generated method stub
                super.onReceivedTitle(view, title);
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.regist_back:
                finish();
                break;
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //发送验证码
            if (flag.equals(PAGEDETAILFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object message = respObj.getMessage();
                if (code.equals("200") && message != null) {
                    InfoDetailBean newsDetailBean = JsonTools.jsonStringToObject(JsonTools.objectToJsonString(message), InfoDetailBean.class);
                    String Url = Constant.Head_string + newsDetailBean.getContent() + Constant.foot_string;//URL编码数据
                    fragment_webView_webView.loadData(Url, "text/html; charset=UTF-8", null);
                } else {
                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }

}
